<?php

namespace api\modules\v1\models\images;

use common\models\Functions;
use common\models\image\Image;
use common\models\Status;
use yii\base\Exception;
/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class Images extends Image
{
    /**
     * 获取图片信息
     * @param int  $position
     * @param int $one
     *
     * @return array
     */
    public function getListByPosition($position = 1, $one = 0) {
        try {
            if (empty($position)) {
                throw new Exception('位置信息不能为空');
            }
            $where['status'] = Status::COMMON_STATUS_SHOW;
            $where['position_id'] = (int) $position;

            $order = 'list_order asc';
            if ($one == 1) {
                $contentList = static::getOneByWhere($where,['id','image'],$order);
                $contentList['image'] = $this->imageHost . $contentList['image'];
            } else {
                $contentList = static::getByWhere($where,['id','image'],$order);

                foreach ($contentList as $k => $v) {
                    $contentList[$k]['id'] = (int) $v['id'];
                    $contentList[$k]['image'] = $this->imageHost . $v['image'];
                }
            }

            return Functions::formatRestAPI(0, '', $contentList);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }


    /**
     * 获取图片信息
     * @param int  $position
     * @param bool $one
     *
     * @return array
     */
    public function getSalary() {
        try {
            $where['status'] = Status::COMMON_STATUS_SHOW;
            $where['position_id'] = (int) 4;

            $order = 'list_order asc';

            $contentList = static::getOneByWhere($where,['id','image', 'url'],$order);
            $res['image'] = $this->imageHost . $contentList['image'];

            return Functions::formatRestAPI(0, '', $res);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
}
